package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.vo.MyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //菜品表里面进行分页查询
        //1、创建mybatisplus分页对象
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //2、使用wrapper封装查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //3、使用mybatisplus提供的分页查询
        page = dishMapper.selectPage(page,wrapper);


        //遍历菜品列表
        List<Dish> dishList = page.getRecords();
        if(CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish : dishList) {
                //获取dish外键categoryId，根据外键获取category
                Category category = categoryMapper.selectById(dish.getCategoryId());
                //设置dish所属的菜品名称
                dish.setCategoryName(category.getName());

                /*//获取当前菜品的口味列表
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper();
                //对比口味列表id和当前菜品的id
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);*/
            }
        }
        return page;
    }

    @Override
    public void add(Dish dish) {
        dishMapper.insert(dish);
        log.info("保存之后的菜品主键：{}",dish.getId());

        //遍历dish实体类从前端接收的口味参数
        List<DishFlavor> flavors = dish.getFlavors();
        if(CollectionUtil.isNotEmpty(flavors)){
            for (DishFlavor flavor : flavors) {
                //为口味设置所属菜品id
                flavor.setDishId(dish.getId());
                //保存口味
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Override
    public Dish findById(Long id) {
        Dish dish = dishMapper.selectById(id);

        //根据菜品的分类id查出分类信息
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        //获取当前菜品的口味列表
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper();
        //对比口味列表id和当前菜品的id
        wrapper1.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
        dish.setFlavors(dishFlavors);

        return dish;
    }

    @Override
    public void update(Dish dish) {
        //更新菜品表信息
        dishMapper.updateById(dish);
        //删除当前口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);

        //添加修改后的口味
        List<DishFlavor> flavors = dish.getFlavors();
        if(CollectionUtil.isNotEmpty(flavors)){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Override
    public void stopAndStartSale(Integer status, MyUtil myUtil) {
        List<Dish> dishes = dishMapper.selectBatchIds(myUtil.getIds());
        if(dishes != null && dishes.size() > 0){
            for (Dish dish : dishes) {
                dish.setStatus(status);
                LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Dish::getId,dish.getId());
                dishMapper.update(dish,wrapper);
            }
        }
    }

    @Override
    public void deleteByIds(MyUtil myUtil) {
        //启用状态计数器
        int count = 0;
        List<Dish> dishes = dishMapper.selectBatchIds(myUtil.getIds());
        if(CollectionUtil.isNotEmpty(dishes)){
            for (Dish dish : dishes) {
                if(dish.getStatus() == 0){
                    //如果为禁售状态则删除菜品并删除口味
                    dishMapper.deleteById(dish.getId());
                    LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(DishFlavor::getDishId,dish.getId());
                    dishFlavorMapper.delete(wrapper);
                }else {
                    count++;
                }
            }
        }
        if(count > 0){
            throw new CustomException(myUtil.getIds().size()-count+"条数据删除成功，当前选中有："+ count+"条启售状态的菜品，启售状态菜品不可删除");
        }
    }

    @Override
    public List<Dish> findByCategoryId(Long id) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //查询出启售状态的菜品
        wrapper.eq(Dish::getCategoryId,id).eq(Dish::getStatus,1);
        List<Dish> list = dishMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<Dish> findList(Long id, Integer status) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,id).eq(Dish::getStatus,status);
        List<Dish> list = dishMapper.selectList(wrapper);

        if(CollectionUtil.isNotEmpty(list)){
            for (Dish dish : list) {
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }

        return list;
    }

    @Override
    public List<Dish> findByName(String name) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Dish::getName,name);
        List<Dish> list = dishMapper.selectList(wrapper);
        return list;
    }

}
